package com.aoxin.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Date;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectRequest;
import com.aoxin.model.dto.OSSDto;

public class OSSUtils {

	/**
	 * 上传文件
	 * 
	 * @param file
	 * @return
	 */
	public static String upload(File file) {
		String type = getSuffix(file);
		type = type.toLowerCase();
		OSS client = new OSSClientBuilder().build(OSSDto.ENDPOINT, OSSDto.ACCESSKEYID, OSSDto.ACCESSKEYSECRET);
		long time = new Date().getTime();
		try {
			client.putObject(new PutObjectRequest(OSSDto.BUCKETNAME, OSSDto.KEY + time + file.getName(), file));
			// System.out.println("Downloading an object");
			OSSObject object = client.getObject(new GetObjectRequest(OSSDto.BUCKETNAME, OSSDto.KEY));
			// System.out.println("Content-Type: " +
			// object.getObjectMetadata().getContentType());
			displayTextInputStream(object.getObjectContent());
		} catch (OSSException oe) {
			oe.printStackTrace();
			System.out.println("Error Message: " + oe.getErrorCode());
			System.out.println("Error Code:       " + oe.getErrorCode());
			System.out.println("Request ID:      " + oe.getRequestId());
			System.out.println("Host ID:           " + oe.getHostId());
		} catch (ClientException ce) {
			System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, "
					+ "such as not being able to access the network.");
			System.out.println("Error Message: " + ce.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			client.shutdown();
		}
		return "https://" + OSSDto.BUCKETNAME + "." + OSSDto.ENDPOINT + "/" + OSSDto.KEY + time + file.getName();
	}

	/**
	 * 删除临时文件
	 *
	 * @param path
	 * @param type
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings("unused")
	private static File createSampleFile(String path, String type) throws IOException {
		File file = File.createTempFile(path, "." + type);
		file.deleteOnExit();
		Writer writer = new OutputStreamWriter(new FileOutputStream(file));
		writer.close();
		return file;
	}

	/**
	 * 上传文件的内容
	 *
	 * @param input
	 * @throws IOException
	 */
	private static void displayTextInputStream(InputStream input) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(input));
		while (true) {
			String line = reader.readLine();
			if (line == null)
				break;
			System.out.println("\t" + line);
		}
		System.out.println();
		reader.close();
	}

	/**
	 * 下载文件到文件path中
	 *
	 * @param path
	 */
	public static void downFile(String path) {
		@SuppressWarnings("deprecation")
		OSSClient ossClient = new OSSClient(OSSDto.ENDPOINT, OSSDto.ACCESSKEYID, OSSDto.ACCESSKEYSECRET);
		ossClient.getObject(new GetObjectRequest(OSSDto.BUCKETNAME, OSSDto.KEY), new File(path));
	}

	/**
	 * 文件类型
	 *
	 * @param file
	 * @return
	 */
	public static String getSuffix(File file) {
		String fileName = file.getName();
		String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
		return suffix;
	}

	public static void main(String[] args) {

	}
}
